/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ContasDialog.java
 *
 * Created on Jun 22, 2011, 11:29:51 AM
 */
package br.com.tbp.gui.financeiro;

import br.com.tbp.gui.MenuPrincipal;
import br.com.tbp.utils.DateUtils;
import br.com.tbp.utils.ImageUtils;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Calendar;
import java.util.HashMap;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;

/**
 *
 * @author Diego
 */
public class ContasDialog extends javax.swing.JDialog {

    private MenuPrincipal menu;
    private HashMap<Integer, MesPanel> mapaDeMeses = new HashMap<Integer, MesPanel>();
    private final int ano = DateUtils.getAnoAtual();
    private final int mes = DateUtils.getMesAtual();

    public ContasDialog(java.awt.Frame parent, String title, boolean modal) {
        super(parent, title, modal);
        initComponents();
        if (parent instanceof MenuPrincipal) {
            this.menu = (MenuPrincipal) parent;
        }
        tabbed.setSelectedIndex(mes);
        redimensionaTela();
        setIconImage(ImageUtils.createIcon("contas.png").getImage());
        setLocationRelativeTo(null);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        conteudo = new javax.swing.JPanel();
        tabbed = new javax.swing.JTabbedPane();
        panelJan = new javax.swing.JPanel();
        panelFev = new javax.swing.JPanel();
        panelMar = new javax.swing.JPanel();
        panelAbr = new javax.swing.JPanel();
        panelMaio = new javax.swing.JPanel();
        panelJun = new javax.swing.JPanel();
        panelJul = new javax.swing.JPanel();
        panelAgo = new javax.swing.JPanel();
        panelSet = new javax.swing.JPanel();
        panelOut = new javax.swing.JPanel();
        panelNov = new javax.swing.JPanel();
        panelDez = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        tituloLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        conteudo.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 3, 3, 1));
        conteudo.setLayout(new java.awt.BorderLayout());

        tabbed.setTabPlacement(javax.swing.JTabbedPane.BOTTOM);
        tabbed.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                tabbedStateChanged(evt);
            }
        });

        panelJan.setLayout(new java.awt.BorderLayout());
        tabbed.addTab("JAN", panelJan);

        panelFev.setLayout(new java.awt.BorderLayout());
        tabbed.addTab("FEV", panelFev);

        panelMar.setLayout(new java.awt.BorderLayout());
        tabbed.addTab("MAR", panelMar);

        panelAbr.setLayout(new java.awt.BorderLayout());
        tabbed.addTab("ABR", panelAbr);

        panelMaio.setLayout(new java.awt.BorderLayout());
        tabbed.addTab("MAIO", panelMaio);

        panelJun.setLayout(new java.awt.BorderLayout());
        tabbed.addTab("JUN", panelJun);

        panelJul.setLayout(new java.awt.BorderLayout());
        tabbed.addTab("JUL", panelJul);

        panelAgo.setLayout(new java.awt.BorderLayout());
        tabbed.addTab("AGO", panelAgo);

        panelSet.setLayout(new java.awt.BorderLayout());
        tabbed.addTab("SET", panelSet);

        panelOut.setLayout(new java.awt.BorderLayout());
        tabbed.addTab("OUT", panelOut);

        panelNov.setLayout(new java.awt.BorderLayout());
        tabbed.addTab("NOV", panelNov);

        panelDez.setLayout(new java.awt.BorderLayout());
        tabbed.addTab("DEZ", panelDez);

        conteudo.add(tabbed, java.awt.BorderLayout.CENTER);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        tituloLabel.setFont(new java.awt.Font("Verdana", 1, 14));
        tituloLabel.setForeground(new java.awt.Color(255, 51, 153));
        jPanel1.add(tituloLabel);

        conteudo.add(jPanel1, java.awt.BorderLayout.PAGE_START);

        getContentPane().add(conteudo, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tabbedStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_tabbedStateChanged
        int tab = tabbed.getSelectedIndex();
        this.carregaContas(tab, ano);
    }//GEN-LAST:event_tabbedStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                ContasDialog dialog = new ContasDialog(new javax.swing.JFrame(),
                        "TBP - Contas", true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel conteudo;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel panelAbr;
    private javax.swing.JPanel panelAgo;
    private javax.swing.JPanel panelDez;
    private javax.swing.JPanel panelFev;
    private javax.swing.JPanel panelJan;
    private javax.swing.JPanel panelJul;
    private javax.swing.JPanel panelJun;
    private javax.swing.JPanel panelMaio;
    private javax.swing.JPanel panelMar;
    private javax.swing.JPanel panelNov;
    private javax.swing.JPanel panelOut;
    private javax.swing.JPanel panelSet;
    private javax.swing.JTabbedPane tabbed;
    private javax.swing.JLabel tituloLabel;
    // End of variables declaration//GEN-END:variables

    private void carregaContas(int mes, int ano) {
        this.carregaTitulo(mes, ano);
        MesPanel mPanel = null;
        mPanel = mapaDeMeses.get(mes);
        if(mPanel == null) {
            System.out.println("carregando panel: " + (mes + 1));
            mPanel = new MesPanel(this.menu, this, (mes + 1), ano);
            JPanel panel = (JPanel) tabbed.getComponentAt(mes);
            panel.add(mPanel, SwingConstants.CENTER);
            panel.repaint();
            mapaDeMeses.put(mes, mPanel);
        }
        mPanel.foco();
    }

    private void carregaTitulo(int mes, int ano) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.MONTH, mes);
        StringBuilder sb = new StringBuilder(DateUtils.getMesComoString(c.getTime())).append(" - ").
                append(ano);
        tituloLabel.setText(sb.toString());
    }

    @Override
    protected JRootPane createRootPane() {
        ActionListener closeListener = new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                //fecharButtonActionPerformed(e);
            }
        };
        KeyStroke stroke =
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        JRootPane myRootPane = new JRootPane();
        myRootPane.registerKeyboardAction(
                closeListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);

        return myRootPane;
    }

    private void redimensionaTela() {
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        setSize(new Dimension(width - 50, height - 200));
    }
}
